package chabernac.android.wordbattle.model;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.util.Log;
import chabernac.android.dictionary.DummyWordProvider;
import chabernac.android.wordbattle.db.iStringDatabase;

public class WordRasterFactory {
	private static Random myRandom = new Random();
	public static final String VOCALS = "AEIOU";
	public static final String NON_VOCALS = "BCDFGHJKLMNPQRSTVWXYZ";

	private final iStringDatabase myStringDatabase;

	public WordRasterFactory(iStringDatabase aStringDatabase){
		myStringDatabase = aStringDatabase;
	}

	private static Player getLocalPlayer(){
		return new Player().setNickName( "Local" );
	}

	public Set<String> convertArrayToSet(String[] aWords){
		Set<String> theWords = new HashSet<String>();
		for(String theWord : aWords) theWords.add(theWord);
		return theWords;
	}

	public WordRaster createBoggleRaster(String aLanguage, int aRows, int aColumns, iStringDatabase aWordValidator){
		WordRaster theRaster = new WordRaster(getLocalPlayer(), aColumns, aRows, aWordValidator, new AllDirectionsSelectionHelper(), aLanguage, new BasicScoreCalculator());
		theRaster.setPlayTime( 60 );
		popuplateWithRestService( theRaster );
//		theRaster.solve();
		return theRaster;
	}

	public WordRaster createSnakeRaster(String aLanguage, int aRows, int aColumns, iStringDatabase aWordValidator){
		WordRaster theRaster = new WordRaster(getLocalPlayer(), aColumns, aRows, aWordValidator, new WordSnakeSelectionHelper(), aLanguage, new BasicScoreCalculator());
		popuplateWithRestService( theRaster );
//		theRaster.solve();
		return theRaster;
	}

	public WordRaster createTestRaster(String aLanguage, int aRows, int aColumns){
		WordRaster theRaster = new WordRaster(getLocalPlayer(), aColumns, aRows, new DummyWordProvider(), new AllDirectionsSelectionHelper(), aLanguage, new BasicScoreCalculator());
		populateRasterRandomly( theRaster );
		theRaster.solve();
		return theRaster;
	}
	
	public void popuplateWithRestService(WordRaster aRaster){
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext localContext = new BasicHttpContext();
		String theURL = "http://wordbattle-server.appspot.com/resources/wordraster/language/" + aRaster.getLanguage() + "/grid/" + aRaster.getRows() + "x" + aRaster.getColumns();
		HttpGet httpGet = new HttpGet(theURL);
		try {
			HttpResponse response = httpClient.execute(httpGet, localContext);
			HttpEntity entity = response.getEntity();
			BufferedReader theReader = new BufferedReader(new InputStreamReader(entity.getContent()));
			for(int row=0;row<aRaster.getRows();row++){
				String theRasterLine = theReader.readLine();
				for(int column=0;column<theRasterLine.length();column++){
					aRaster.setLetter(column, row, theRasterLine.charAt(column));
				}
			}
			String[] theWords = theReader.readLine().split(";");
			aRaster.setWordsInRaser(convertArrayToSet(theWords));
		} catch (Exception e) {
			Log.e(WordRasterFactory.class.getName(), "Could not create raster from rest service", e);
		}
	}

	private void populateRasterRandomly(WordRaster aRaster, String aLanguage){
		try{
			RandomLetterHelper theRandomLetterHelper = myStringDatabase.getRandomLetterHelper(aLanguage);
			for(int row = 0; row < aRaster.getRows(); row++){
				for(int column = 0; column < aRaster.getColumns(); column ++){
					aRaster.setLetter(column, row, theRandomLetterHelper.getRandomLetter());
				}
			}
		}catch(Exception e){
			Log.e( WordRasterFactory.class.getName(), "Error occured when populating raster", e );
		}
	}

	private static void populateRasterRandomly(WordRaster aRaster) {
		for(int row = 0; row < aRaster.getRows(); row++){
			for(int column = 0; column < aRaster.getColumns(); column ++){
				aRaster.setLetter(column, row, (char)('A' + Math.abs(myRandom.nextInt()) % 26));
			}
		}
	}
}
